package com.heima.controller;

import com.heima.pojo.dto.RegionDto;
import com.heima.pojo.vo.RegionVo;
import com.heima.service.RegionService;
import com.heima.utils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 赵超
 */
@RestController
@Slf4j
@RequestMapping("/api/vm-service/region")
public class RegionController {
    @Autowired
    private RegionService regionService;

    @GetMapping("/search")
    public PageResult<RegionVo> page(
            @RequestParam Integer pageIndex,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String name
    ) {
        return regionService.page(pageIndex, pageSize, name);
    }

    @PostMapping
    public Boolean add(@RequestBody RegionDto regionDto) {
        return regionService.add(regionDto) > 0;
    }

    @PutMapping("/{regionId}")
    public Boolean update(@PathVariable String regionId, @RequestBody RegionDto regionDto) {
        return regionService.update(Long.valueOf(regionId), regionDto) > 0;
    }

    @DeleteMapping("/{regionId}")
    public Boolean delete(@PathVariable String regionId) {
        return regionService.delete(Long.valueOf(regionId)) > 0;
    }


}
